/* JS per la pagina del corso acquistato/non acquistato */



/* Variabili Json */
var pathInteressi            =      "json/altri_interessi.json";
var pathRiempimentoDomande   =      "json/domande.json";




/***************************************************************************************************************************/
/***************************************************************************************************************************/

//                                    GESTIONE RIEMPIMENTO CONTENUTI BOX "POTREBBE INTERESSARTI"

/***************************************************************************************************************************/
/***************************************************************************************************************************/



function fmy_riempiPotrebbeInteressarti(){
    $.ajax({
        url: pathInteressi,
        dataType: "json",
        success: function( dataJson ) {
            //console.log( dataJson );
            
            sizeDataJson = ( dataJson.length );
            //  console.log(sizeDataJson);
            
            if (sizeDataJson > 0 ) 
                $( ".potrebbeInteressarti" ).css( "display", "block" );
            
                      
            var lastInteresse = $( ".potrebbeInteressarti ul.box-interesse li.item:last-child" );
            
            for( i = 0 ; i < sizeDataJson ; i++ ){
            
                //creazione variabili
                var corsoUrl         =   dataJson[i].indirizzo_url;
                var copertinaUrl     =   dataJson[i].copertina;
                var titolo           =   dataJson[i].titolo;
                var livello          =   dataJson[i].livello;
                var prezzo           =   dataJson[i].prezzo;
                var voto             =   dataJson[i].voto;
                
                /*inserimento dei dati negli li di lista_domande */
                lastInteresse.find( "a" ).attr( "src", corsoUrl );
                lastInteresse.find( "img" ).attr( "src", copertinaUrl );
                lastInteresse.find( ".title_int" ).html( titolo );
                lastInteresse.find( ".level" ).html( livello );
                lastInteresse.find( ".price" ).html( prezzo );
                
                lastInteresse.find( ".lampadine_header_xs" ).html( voto );
                new_voto = voto*2;
				new_voto = parseInt( new_voto );
				switch( new_voto ){
					case 0:
						lastInteresse.find(".lampadine_header_xs").css("background-position","0px -170px");
					break;
					case 1:
						lastInteresse.find(".lampadine_header_xs").css("background-position","0px -152px");
					break;
					case 2:
						lastInteresse.find(".lampadine_header_xs").css("background-position","0px -136px");
					break;
					case 3:
						lastInteresse.find(".lampadine_header_xs").css("background-position","0px -117px");
					break;			  
					case 4:
						lastInteresse.find(".lampadine_header_xs").css("background-position","0px -100px");
					break;
					case 5:
						lastInteresse.find(".lampadine_header_xs").css("background-position","0px -84px");
					break;		
                    case 6:
						lastInteresse.find(".lampadine_header_xs").css("background-position","0px -66px");
					break;
                    case 7:
						lastInteresse.find(".lampadine_header_xs").css("background-position","0px -49px");
					break;
                    case 8:
						lastInteresse.find(".lampadine_header_xs").css("background-position","0px -32px");
					break;
                    case 9:
						lastInteresse.find(".lampadine_header_xs").css("background-position","0px -15px");
					break;
                    case 10:
						lastInteresse.find(".lampadine_header_xs").css("background-position","0px 0px");
					break;
				}
                if( i !== ( sizeDataJson - 1 ) ) {
                    $( "ul.box-interesse li.item:first-child" ).clone().appendTo( "ul.box-interesse" );
				}	
            } //end for
            $( "ul.box-interesse li.item:first-child" ).clone().appendTo( "ul.box-interesse" );
            $( "ul.box-interesse li.item:first-child" ).remove();
            
        },//end success
        
        error: function() {
         console.log( "non ho caricato il file json" );
        } //end error
        
    });
} /* end riempimento tab attivi*/



/***************************************************************************************************************************/
/***************************************************************************************************************************/

//                                    GESTIONE RIEMPIMENTO CONTENUTI BOX "DOMANDE / RISPOSTE"

/***************************************************************************************************************************/
/***************************************************************************************************************************/


function fmy_riempiBoxDomande(){
    $.ajax({
        url: pathRiempimentoDomande,
        dataType: "json",
        success: function( dataJson ) {
            //console.log( dataJson );

            // riempimento dinamico degli elementi dell'html
            var lastDomanda = $( ".sez-right .box_domande ul.lista_domande li.domanda:last-child" );
            
            sizeDataJson = ( dataJson.length );
            
            
            for( i = 0 ; i < sizeDataJson ; i++ ){
            
                //creazione variabili
                var nomeAutoreDomanda           =   dataJson[i].nomeAutoreDomanda;
                var dataPubblicazioneRisposta   =   dataJson[i].dataPubblicazioneRisposta;
                var testoDomanda                =   dataJson[i].testoDomanda;
                var testoRisposta               =   dataJson[i].testoRisposta;
            
                /*inserimento dei dati negli li di lista_domande */
                lastDomanda.find( ".question .title span.autore" ).html( nomeAutoreDomanda + " chiede");
                lastDomanda.find( ".question .title span.date" ).html( dataPubblicazioneRisposta );
                lastDomanda.find( ".question p" ).html( testoDomanda );
                lastDomanda.find( ".answer p" ).html( testoRisposta );
            
                if( i !== ( sizeDataJson - 1 ) ) {
                    $( "ul.lista_domande li.domanda:first-child" ).clone().appendTo( "ul.lista_domande" );
				}	
            } //end for
            $( ".sez-right .box_domande ul.lista_domande li.domanda:first-child" ).clone().appendTo( "ul.lista_domande" );
            $( ".sez-right .box_domande ul.lista_domande li.domanda:first-child" ).remove();
            
        },//end success
        
        error: function() {
         console.log( "non ho caricato domande.json" );
        } //end error
        
    });
} 




/***************************************************************************************************************************/
/***************************************************************************************************************************/

//                                    GESTIONE SIDEBAR PER IL BOX DOMANDE ALL'AUTORE

/***************************************************************************************************************************/
/***************************************************************************************************************************/


function fmy_scroll_ListaDomande() {
    scroll_listaDomande = false;
    scroll_listaDomande = $(".box_domande .lista_domande").niceScroll({cursorcolor:"#52B5BB", cursorfixedheight: "50"});
}



/***************************************************************************************************************************/
/***************************************************************************************************************************/

//                                    GESTIONE SIDEBAR PER IL BOX AUTORI

/***************************************************************************************************************************/
/***************************************************************************************************************************/


function fmy_scroll_ListaAutori() {
    scroll_listaAutori = false;
    scroll_listaAutori = $( ".box_writers .box_author .many_teachers" ).niceScroll({cursorcolor:"#52B5BB", cursorfixedheight: "50"});
}





/***************************************************************************************************************************/
/***************************************************************************************************************************/

//                                    VISUALIZZA/NASCONDE TUTTI GLI AUTORI

/***************************************************************************************************************************/
/***************************************************************************************************************************/
function fmy_box_author_viewall() {
    $( "#box_author_viewall" ).click( function () {
        fmy_scroll_ListaAutori();
        $(this).css( "display", "none" );
        $(this).next( ".single_teacher" ).slideUp( "1000" );
        $(this).next().next( ".many_teachers" ).slideDown( "1000" );
        $( ".go_back_oneTeacher" ).css( "display", "block" );
    }); 
}



function fmy_box_author_goBack() {
    $( ".go_back_oneTeacher" ).click( function () {
        $(this).css( "display", "none" );
        $(this).parent( ".many_teachers" ).slideUp( "1000" ); 
        $(this).parent().prev( ".single_teacher" ).slideDown( "1000" );
        $( "#box_author_viewall" ).css( "display", "block" );
    }); 
}


/***************************************************************************************************************************/
/***************************************************************************************************************************/

//                                    VISUALIZZA TUTTI GLI AUTORI

/***************************************************************************************************************************/
/***************************************************************************************************************************/
function fmy_showHideInfoAutore() {

    $( ".many_teachers .show_icon" ).click(function(){
  	  $(this).next().slideToggle();
	  
	  if ( $(this).hasClass("icon_plus") )
        $(this).removeClass( "icon_plus" ).addClass( "icon_minus" );
	  else
        $(this).removeClass( "icon_minus" ).addClass( "icon_plus" );
    });

}




/***************************************************************************************************************************/
/***************************************************************************************************************************/

//                                    AGGIORNA DINAMICAMENTE LA % DI LEZIONE RAGGIUNTA

/***************************************************************************************************************************/
/***************************************************************************************************************************/


function fmy_updateHeight () {
    percent = $( ".corso_percentuale .percentage span" ).text();
    maxHeight = $( ".corso_percentuale .bkg_categoria" ).height();
    console.log(percent);
    console.log(maxHeight);
    val = ( maxHeight / 100 ) * percent;
    $( "#bar" ).height( val );
}


/*

function fmy_updateHeight ( p_percent, p_maxHeight ) {
    val = ( p_maxHeight / 100 ) * p_percent;
    $( "#bar" ).height( val );
}

*/



/***************************************************************************************************************************/
/***************************************************************************************************************************/

//                   DOCUMENT READY

/***************************************************************************************************************************/
/***************************************************************************************************************************/



$(document).ready(function(){

    //gestione riempimento box Potrebbe Interessarti
    fmy_riempiPotrebbeInteressarti();
    //gestione riempimento box PDomande
    fmy_riempiBoxDomande();    
    fmy_scroll_ListaDomande();
    
    //gestione visualizzazione autori
    //fmy_box_author_viewall();
    //fmy_box_author_goBack ();
  
    //hide/show descrizione >= 3 autori
    fmy_showHideInfoAutore();
    //scrolling sezione autori con >=3autori
    fmy_scroll_ListaAutori();
    
    // calcolo dell'altezza dell'immagine con %
    fmy_updateHeight();
    
});

